package com.rc.evcharger.service.hlht;


import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.mapper.discount.TenantDiscountPolicyMapper;
import com.rc.evcharger.mapper.elec.ElecStationPriceMapper;
import com.rc.evcharger.mapper.hlht.HlhtOperatorSettingMapper;
import com.rc.evcharger.mapper.hlht.HlhtTenantOpenPriceMapper;
import com.rc.evcharger.mapper.order.OrderDetailMapper;
import com.rc.evcharger.model.elec.ElecStationPrice;
import com.rc.evcharger.model.hlht.HlhtOperatorSetting;
import com.rc.evcharger.model.hlht.HlhtTenantOpenPrice;
import com.rc.evcharger.model.order.OrderDetail;
import com.rc.evcharger.model.order.OrderInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Create by Yang on 2021/5/29
 */
@Service
public class HlhtTenantOpenPriceServiceImpl implements HlhtTenantOpenPriceService {

    private static Logger logger = LogManager.getLogger(HlhtTenantOpenPriceServiceImpl.class);

    @Autowired
    private HlhtTenantOpenPriceMapper tlhtTenantOpenPriceMapper;

    @Autowired
    private ElecStationPriceMapper elecStationPriceMapper;

    @Autowired
    private HlhtOperatorSettingMapper hlhtOperatorSettingMapper;

    @Autowired
    private TenantDiscountPolicyMapper tenantDiscountPolicyMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public HlhtTenantOpenPriceMapper getMapper() {
        return tlhtTenantOpenPriceMapper;
    }


    @Override
    public HlhtTenantOpenPrice selectByPriceIdAndTenantCode(String tenantCode, Long elecStationId, String hlhtOperatorId) {
        ElecStationPrice elecStationPrice = elecStationPriceMapper.queryValidElecStationPrice(elecStationId);
        if (elecStationPrice == null) {
            return null;
        }
        return tlhtTenantOpenPriceMapper.selectByPriceIdAndTenantCode(tenantCode, elecStationPrice.getPriceId(), elecStationId, hlhtOperatorId);
    }

    /**
     * 互联互通调整价后,再计算折扣
     *
     * @param orderInfo
     */
    @Override
    public void adjustHlhtRelif(OrderInfo orderInfo) {
        if (orderInfo.getPayAmount().compareTo(BigDecimal.ZERO) == 0) {
            return;
        }

        //TlhtTenantOpenPrice tlhtTenantOpenPrice = this.selectByPriceIdAndTenantCode(orderInfo.getTenantCode(), orderInfo.getElecStationId(), orderInfo.getHlhtOperatorId());
        //
        //if (tlhtTenantOpenPrice == null) {
        //    return;
        //    //反向互联互通折扣
        //}
        //调整系数
        //if (UserLevelConstant.DISCOUNT_TYPE_ACTIVITY.equals(tlhtTenantOpenPrice.getServicePriceAdjustType())) {
        //} else {
        //    //调整折扣暂不处理
        //    return;
        //}
        //调整总金额
        BigDecimal adjustServiceMoney = BigDecimal.ZERO;

        //累计调整服务费金额  调整单价*电量
        List<OrderDetail> orderDetails = orderDetailMapper.listByOrderId(orderInfo.getOrderId());
        for (OrderDetail orderDetail : orderDetails) {
            HlhtTenantOpenPrice tlhtTenantOpenPrice = tlhtTenantOpenPriceMapper.selectByBeginTime(orderInfo.getElecStationId(), orderInfo.getTenantCode(), orderInfo.getHlhtOperatorId(), orderDetail.getBeginTime());
            if (tlhtTenantOpenPrice == null){
                continue;
            }
            adjustServiceMoney = adjustServiceMoney.add(tlhtTenantOpenPrice.getServicePriceAdjustValue().multiply(orderDetail.getPower()));
        }

        //得到调整后的价格服务费价格
        orderInfo.setServiceAdjustAmount(orderInfo.getTotalSeviceMoney().add(adjustServiceMoney));

        //调整后服务费差额
        orderInfo.setAdjustServiceDiffAmount(adjustServiceMoney);
        //调整后总金额
        orderInfo.setAdjustTotalMoney(orderInfo.getTotalElecMoney().add(orderInfo.getServiceAdjustAmount()));
        //调整后总金额(注：还未享受折扣)
        orderInfo.setAdjustPayAmount(orderInfo.getAdjustTotalMoney());
        orderInfo.setServiceAdjustDiscAmount(BigDecimal.ZERO);

    }

    /**
     * 返回调整单价
     *
     * @param orderInfo
     * @param priceType 0.系数单价 1.服务费,系数单价的和
     * @return
     */
    @Override
    public String selectHlhtAdjustServicePrices(OrderInfo orderInfo, String priceType) {
        List<HlhtTenantOpenPrice> tlhtTenantOpenPrices = tlhtTenantOpenPriceMapper.listByEsIdAndHlhtInfo(orderInfo.getElecStationId(), orderInfo.getTenantCode(), orderInfo.getHlhtOperatorId());
        if (CollectionUtils.isEmpty(tlhtTenantOpenPrices)) {
            logger.error("无效hlht电价对应站点ID{},会员ID,:{},:{}", orderInfo.getElecStationId(), orderInfo.getMemberId());
            return "";
        }

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        StringBuffer sb = new StringBuffer();

        for (HlhtTenantOpenPrice tlhtTenantOpenPrice : tlhtTenantOpenPrices) {
            ElecStationPrice elecStationPrice = elecStationPriceMapper.selectByPrimaryKey(tlhtTenantOpenPrice.getPriceId());

            //调整系数单价
            if (SocketConstant.STR_CON0.equals(priceType)) {
                sb.append(sdf.format(elecStationPrice.getBeginTime())).append("~")
                        .append(sdf.format(elecStationPrice.getEndTime())).append(" ")
                        .append(tlhtTenantOpenPrice.getServicePriceAdjustValue()).append("元/度")
                        .append("#");
            } else {
                BigDecimal add = tlhtTenantOpenPrice.getServicePriceAdjustValue().add(elecStationPrice.getServicePrice());
                //调整系数+服务费之和
                sb.append(sdf.format(elecStationPrice.getBeginTime())).append("~")
                        .append(sdf.format(elecStationPrice.getEndTime())).append(" ")
                        .append(add).append("元/度")
                        .append("#");
            }
        }


        return sb.toString().substring(0, sb.length() - 1);
    }

    @Override
    public BigDecimal selectServicePriceAdjustValue(OrderInfo orderInfo, String memberType) {
        //只针对互联互通用户
        if (!SocketConstant.STR_CON3.equals(memberType) || StringUtils.isEmpty(orderInfo.getHlhtOperatorId())) {
            return null;
        }

        HlhtTenantOpenPrice tlhtTenantOpenPrice = this.selectByPriceIdAndTenantCode(orderInfo.getTenantCode(), orderInfo.getElecStationId(), orderInfo.getHlhtOperatorId());

        if (tlhtTenantOpenPrice == null) {
            HlhtOperatorSetting hlhtOperatorSetting = hlhtOperatorSettingMapper.selectTenantCodeAndHlhtOperatorId(orderInfo.getTenantCode(), orderInfo.getHlhtOperatorId());
            if (hlhtOperatorSetting == null) {
                return null;
            }
            if (SocketConstant.STR_CON1.equals(hlhtOperatorSetting.getServicePriceAdjustType())) {
                return hlhtOperatorSetting.getServicePriceAdjustValue();
            }
            return null;
            //反向互联互通折扣
        } else if (SocketConstant.STR_CON1.equals(tlhtTenantOpenPrice.getServicePriceAdjustType())) {
            return tlhtTenantOpenPrice.getServicePriceAdjustValue();
        }
        return null;
    }
}
